/*

 ============================================================================
 Name        : C.c
 Author      : 
 Version     :
 Copyright   : Your copyright notice
 Description : Hello World in C, Ansi-style
 ============================================================================



#include <stdio.h>
#include <stdlib.h>




int main(void) {
	printf("!!!Hello World!!!");  prints !!!Hello World!!!
	getValues();
	return EXIT_SUCCESS;
}

void getValues(void)
{
	int m;
	getch();
	printf("enter the value for m: \n");
	scanf("%d",&m);
	printf("entered value for m is :%d",m);
}
*/

#include <stdio.h>
#include <stdlib.h>

// struct is simple, all you need is a node
typedef struct node {
	int *S, size, last_index;
	struct node* next;
} NODE;


// create your node with a function
// here we also point it at the stack head
// and return new stack head

NODE *push(NODE *stack, int size, int last_index) {
	NODE *node = malloc(sizeof(NODE));
	node->size = size;
	node->last_index = last_index;
	node->next = stack;
	node->S = malloc(sizeof(int)*size);
	printf ("\n %d %d ", size, last_index);
	return node;
}

void print_sub_set(NODE *n, int count) {
	int h;
	printf("printing sub set");
	printf("%d)\t\t{", count);
	for(h=0;h<n->size;h++) {
		printf("%d",n->S[h]);
		if(h!=n->size-1) { printf(","); }
	}
	printf("}\n");
}

void calculate_power_set(int *set, int SIZE) {
	int i, j, count = 0;
	NODE *stack = NULL;

	// populate your stack
	// now you don't have to do that recursive call
	for(i=0;i<SIZE;i++) {
		stack = push(stack, 1, i);
		stack->S[0]=set[i];
	}

	printf("\tP(S)={\n%d)\t\t{},\n",++count);
	while(stack!=NULL) {
		// this is a POP
		NODE *item = stack; // grab the current head
		stack = stack->next; // remove head from stack

		print_sub_set(item, ++count);

		for(i=item->last_index;i<SIZE-1;i++){
			stack = push(stack, item->size+1, i+1);
			for(j=0; j<item->size; j++)
			{
				//printf("item : %d \n",item->S[j]);
				stack->S[j] = item->S[j];
			}
			stack->S[j]=set[i+1];
		}
		free(item->S);
		free(item);
	}
	printf("\t};\n");

	printf("values in power set are \n");

	NODE *list=stack;
	while(list!=NULL)
	{
		int i=0;

		for(i=0;i<list->size;i++)
		{
			printf("\t");
			printf("%d",list->S[i]);
		}
		printf("\n");
		list=list->next;

	}
}

/*
int main(int argc, char** argv) {
	int set[]={1,2,3,4};
	printf("size : %d ,%d,%d ",sizeof(set),sizeof(int),sizeof(set)/sizeof(int));
	calculate_power_set(set, sizeof(set)/sizeof(int));


	return 0;
}
*/

/*

#include <stdio.h>
#include <stdlib.h>


int main(int argc, char **argv)
{
	int m,i;
	int set[3];

	printf("Enter the set size\n");
	scanf("%d",&m);

	printf("Enter the %d values for the set\n",m);
	for(i=0;i<3;i++)
	{
		scanf("%d",&set[i]);
	}
	printf("Entered values for set are");
	for(i=0;i<3;i++){
		printf("%d",set[i]);
	}
	return 1;
}


void getPowerset(int set[])
{
	int *pset[8],i;
	for(i=0;i<8;i++)
	{
		pset[i]=malloc(sizeof(int)*3);
	}
	int subset[3];
	createPowerSet(set,subset,pset,3,0);

}


void createPowerSet(int set[], int subset[], int *pset[], int size, int start)
{
	int i,ele;
	for(i=start;i<size;i++)
	{
		ele=set[i];
		subset[i]=ele;
		pset[i]=subset;
		if(i<size-1)
		{
			createPowerSet(set,subset)
		}
	}
}


*/
